class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if(0 == matrix.length) {
            return false;
        }
        //有序就用二分
        boolean flag = false;
        //i控制从上往下的行
        int i = 0;
        //j控制从后往前的列
        int j = matrix[0].length - 1;
        int len = matrix.length;
        while(i < len && j >= 0) {
            if(target > matrix[i][j]) {
                i++;
            }else if (target < matrix[i][j]) {
                j--;
            }else {
                return true;
            }
        }
        
        return false;
    }
    public boolean findNumberIn2DArray1(int[][] matrix, int target) {
        if(0 == matrix.length) {
            return false;
        }
        //有序就用二分
        boolean flag = false;
        int i = 0;
        int len = matrix.length;
        for(i = 0;i<len;i++) {
            int left = 0;
            int right = matrix[i].length-1;
            while(left<=right) {
                int mid = (left+right)/2;
                if(target > matrix[i][mid]) {
                    left = mid + 1;
                }else if(target < matrix[i][mid]) {
                    right = mid - 1;
                }else {
                    return true;
                }
            }
        }
        return false;
    }
}